package com.plan.controller.controller;

import com.plan.commen.bean.layuiBean.VoPage;
import com.plan.commen.entity.ApiResult;
import com.plan.commen.entity.BaseController;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.plan.commen.bean.layuiBean.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plan.commen.bean.User;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-11-30
 */
@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {

     /** 主页面 */
    @RequestMapping("main.html")
    public String main(){
       return  "/user/userMain";
    }

    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "/add.html")
    public String addUserHtml( ){
        return "/user/userAdd";
    }

    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/edit.html")
    public String editUserHtml(String id ,Model model ){
       model.addAttribute("user",userService.getById(id));
       return "/user/userEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "/show.html")
    public String showUserHtml(String id ,Model model ){
        model.addAttribute("user",userService.getById(id));
        return "/user/userShow";
    }


    /** 分页查询User
     * @param user  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "/findUserByPage")
    @ResponseBody
    public ResultPage findUserByPage(VoPage voPage, User user){
        return userService.findUserByPage(voPage,user);
    }

    /** 添加User
     * @param user  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/saveOrUpdateUser")
    @ResponseBody
    public ApiResult saveOrUpdateByIdUser(@RequestBody User user  ){
        if(userService.saveOrUpdate(user)){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }





    /** 删除一条User
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "/del_User")
    @ResponseBody
    public ApiResult delUser( String id  ){
        if(userService.removeById(id)){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }


}
